import { envUtil } from './imp/utils/env-util'
import { E_ENV } from './typing/enum/service-enum'
import { E_LOG_LEVEL } from './typing/service/ILogService'

export interface IServerDomainConfig {
  apiDomain: string
}

export interface IServiceConfig {
  logLevel?: E_LOG_LEVEL
  env?: E_ENV
  serverDomain?: IServerDomainConfig
  isDebug?: boolean
}

const defaultConfig: Partial<IServiceConfig> = {
  logLevel: E_LOG_LEVEL.ALL,
  env: E_ENV.UAT,
  isDebug: false
}

let serviceConfig: IServiceConfig = defaultConfig

export function buildServiceConfig(config: IServiceConfig): IServiceConfig {
  const env: E_ENV = (config.env || defaultConfig.env) as E_ENV
  const domainConfig = envUtil.getServerDomain(env)

  serviceConfig = {
    ...defaultConfig,
    ...{
      serverDomain: domainConfig
    },
    ...config
  }

  return serviceConfig
}

export function getServiceConfig(): IServiceConfig {
  return serviceConfig
}
